package com.hui.wanxin.web.common.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.hui.wanxin.web.common.OBS.ObsUtils;
import com.hui.wanxin.web.common.mq.entity.SearchEntity;
import com.hui.wanxin.web.common.service.CommonService;
import com.hui.wanxin.web.entity.Dictionary;
import com.hui.wanxin.web.service.DictionaryService;
import com.hui.wanxin.web.service.PopularScienceService;
import com.hui.wanxin.web.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * @author: 灰灰
 * @date: 2023年10月14日 12:28
 * @ClassName: CommonServiceImpl
 * @Description: TODO
 */
@Service
@Slf4j
public class CommonServiceImpl implements CommonService {


    @Autowired
    private ObsUtils obsUtils;

    @Autowired
    private SmsServiceImpl smsService;
    //新闻
    @Autowired
    private PopularScienceService popularScienceService;
    //字典
    @Autowired
    private DictionaryService dictionaryService;

    @Override
    public String upload(MultipartFile multipartFile) {
        try {
            return obsUtils.upload(multipartFile);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public void sendCode(String phone, Integer code) {
        smsService.sendCode(phone, code);
    }

    @Override
    public R search(String content) {

        List<SearchEntity> result = new ArrayList<>();

        List<Dictionary> dictionaries = dictionaryService.list();
        //多线程查询
        if (CollUtil.isNotEmpty(dictionaries)) {


        }

        return R.success(result);
    }

    public List<String> selectTheKey(String content, List<Dictionary> dictionaries) {
        List<String> result = new ArrayList<>();
        int len = Math.min(dictionaries.size(), 500);
        //多线程处理查询关键词
        int x = 0;
        int y = len;
        if(dictionaries.size() > 1000){
            int theadSize = dictionaries.size() / 1000;
            ExecutorService executor =new ScheduledThreadPoolExecutor(theadSize);
            for (int i = 0; i < dictionaries.size(); i+=len) {
                int finalX = x;
                int finalY = y;
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        List<Dictionary> subList = dictionaries.subList(finalX, finalY);
                        for (Dictionary dictionary : subList) {
                            if(content.contains(dictionary.getWork())){
                                result.add(dictionary.getWork());
                            }
                        }
                    }
                });
                y+=len;
                x+=len;
                if(y>dictionaries.size()){
                    y=dictionaries.size();
                }
            }
            executor.shutdown();
        }else{
            for (int i = 0; i < dictionaries.size(); i+=len) {
                List<Dictionary> subList = dictionaries.subList(x, y);
                for (Dictionary dictionary : subList) {
                    if(content.contains(dictionary.getWork())){
                        result.add(dictionary.getWork());
                    }
                }
                y+=len;
                x+=len;
                if(y>dictionaries.size()){
                    y=dictionaries.size();
                }
            }
        }
       

        return result;
    }
}
